package org.jeecg.sms.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Slf4j
public class SmsUtils {

    /**
     * x-pmc-biz-id
     * @return
     */
    public static String generateScenarioId() {
        String sHigh = Long.toHexString(System.currentTimeMillis() / 1000);

        String sLow = null;
        try {
            sLow = RandomStringUtils.random(24, new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'});;
        } catch (Exception e) {
            log.error("Generating SpanID failed, error=" + e.getMessage());
            return null;
        }
        return sHigh + sLow;
    }

    /**
     * x-pmc-sign
     * @param bizID
     * @param secret
     * @param timeStamp
     * @return
     */
    public static String getSign(String bizID, String secret, String timeStamp){
        try {
            MessageDigest alga = MessageDigest.getInstance("SHA-256");
            alga.update((bizID + secret + timeStamp).getBytes());
            byte[] digest = alga.digest();
            return Base64.getEncoder().encodeToString(digest);
        } catch (NoSuchAlgorithmException ex) {
            log.error("getSign异常",ex);
            return null;
        }
    }
}
